package com.platform.service.impl;

import com.github.pagehelper.PageInfo;
import com.platform.annotation.SysLog;
import com.platform.base.BaseService;
import com.platform.common.Const;
import com.platform.common.ServerResponse;
import com.platform.pojo.Psaa0001;
import com.platform.pojo.User;
import com.platform.service.IPsaa0001Service;
import com.platform.util.DateTimeUtil;
import com.platform.util.MapperUtil;
import com.platform.util.NumberUtil;
import com.platform.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 招标人信息服务层
 */
@Service("iPsaa0001Service")
public class Psaa001ServiceImpl extends BaseService implements IPsaa0001Service {

    /**
     * 条件查询招标人
     * @param map
     * @return
     */
    @SysLog("查询招标人")
    public ServerResponse<PageInfo> selectByParamMap(Map map){
        return MapperUtil.pageInfo(map,psaa0001Mapper);
    }

    /**
     * 根据用户名查询招标人详细信息
     * @param userName
     * @return
     */
    @SysLog("查询招标人详细信息")
    public ServerResponse<Map> selectInfoByUserName(String userName){
        if(StringUtil.isEmpty(userName)){
            return ServerResponse.createByErrorMessage("用户名不能为空");
        }
        Map resultMap = psaa0001Mapper.selectInfoByUserName(userName);
        //调整日期格式
        if(StringUtil.isNotEmpty(StringUtil.valueOfString(resultMap.get("busi_start_date")))){
            resultMap.put("busi_start_date",DateTimeUtil.dateToStr((Date) resultMap.get("busi_start_date")));
        }
        if(StringUtil.isNotEmpty(StringUtil.valueOfString(resultMap.get("busi_end_date")))){
            resultMap.put("busi_end_date",DateTimeUtil.dateToStr((Date) resultMap.get("busi_end_date")));
        }
        if(StringUtil.isNotEmpty(StringUtil.valueOfString(resultMap.get("busi_operation_start_date")))){
            resultMap.put("busi_operation_start_date",DateTimeUtil.dateToStr((Date) resultMap.get("busi_operation_start_date")));
        }
        if(StringUtil.isNotEmpty(StringUtil.valueOfString(resultMap.get("busi_operation_end_date")))){
            resultMap.put("busi_operation_end_date",DateTimeUtil.dateToStr((Date) resultMap.get("busi_operation_end_date")));
        }
        if(StringUtil.isNotEmpty(StringUtil.valueOfString(resultMap.get("lcns_prctc_start_date")))){
            resultMap.put("lcns_prctc_start_date",DateTimeUtil.dateToStr((Date) resultMap.get("lcns_prctc_start_date")));
        }
        if(StringUtil.isNotEmpty(StringUtil.valueOfString(resultMap.get("lcns_prctc_end_date")))){
            resultMap.put("lcns_prctc_end_date",DateTimeUtil.dateToStr((Date) resultMap.get("lcns_prctc_end_date")));
        }
        resultMap.put("company_institution",StringUtil.valueOfString(resultMap.get("company_institution")));
        resultMap.put("three_in_one_flag",StringUtil.valueOfString(resultMap.get("three_in_one_flag")));
        resultMap.put("ownership",StringUtil.valueOfString(resultMap.get("ownership")));
        resultMap.put("medical_type",StringUtil.valueOfString(resultMap.get("medical_type")));
        if(null != resultMap){
            return ServerResponse.createBySuccess(Const.Msg.SELECTSUCCESS,resultMap);
        }
        return ServerResponse.createByErrorMessage(Const.Msg.SELECTERROR);
    }

    /**
     * 更新招标人信息
     * @param paramMap
     * @return
     */
    @SysLog("更新招标人信息")
    public ServerResponse<String> update(Map paramMap, HttpServletRequest request){
        //查询版本号是否一致
        int oldVersion = NumberUtil.safeToNumber(paramMap.get("version"));
        int id = NumberUtil.safeToNumber(paramMap.get("id"));
        if(id == 0){
            return ServerResponse.createByErrorMessage(Const.Msg.PARAMTERERROR);
        }
        if(!MapperUtil.selectVersionIsChangeById(id,oldVersion,psaa0001Mapper)){
            return ServerResponse.createByErrorMessage(Const.Msg.DATAISUPDATE);
        }
        //获取操作人
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null == o){
            return ServerResponse.createByErrorMessage(Const.Msg.USERNOLOGIN);
        }
        User user = (User)o;
        if(null == user){
            return ServerResponse.createByErrorMessage(Const.Msg.USERNOLOGIN);
        }
        Psaa0001 psaa0001 = new Psaa0001();
        psaa0001.setId(id);
        psaa0001.setUpdateUser(user.getUsername());
        psaa0001.setCustomerName(StringUtil.valueOfString(paramMap.get("customer_name")));//医疗机构名称
        psaa0001.setCompanyInstitution(NumberUtil.safeToNumber(paramMap.get("company_institution")));//性质
        psaa0001.setRegisterAssets(StringUtil.valueOfString(paramMap.get("register_assets")));//注册资金
        psaa0001.setThreeInOneFlag(NumberUtil.safeToNumber(paramMap.get("three_in_one_flag")));//是否三证合一
        psaa0001.setUscCode(StringUtil.valueOfString(paramMap.get("usc_code")));//统一社会信用代码
        psaa0001.setLawer(StringUtil.valueOfString(paramMap.get("lawer")));//法定代表人
        String busiStartDate = StringUtil.valueOfString(paramMap.get("busi_start_date"));//营业期限开始
        if(StringUtil.isNotEmpty(busiStartDate)){
            psaa0001.setBusiStartDate(DateTimeUtil.strToDate(busiStartDate));
        }
        String busiEndDate = StringUtil.valueOfString(paramMap.get("busi_end_date"));//营业期限结束
        if(StringUtil.isNotEmpty(busiEndDate)){
            psaa0001.setBusiEndDate(DateTimeUtil.strToDate(busiEndDate));
        }
        String busiOperationStartDate = StringUtil.valueOfString(paramMap.get("busi_operation_start_date"));//机构有效期（开始）
        if(StringUtil.isNotEmpty(busiOperationStartDate)){
            psaa0001.setBusiOperationStartDate(DateTimeUtil.strToDate(busiOperationStartDate));
        }
        String busiOperationEndDate = StringUtil.valueOfString(paramMap.get("busi_operation_end_date"));//机构有效期（结束）
        if(StringUtil.isNotEmpty(busiOperationEndDate)){
            psaa0001.setBusiOperationEndDate(DateTimeUtil.strToDate(busiOperationEndDate));
        }
        psaa0001.setBusiOperationNum(StringUtil.valueOfString(paramMap.get("busi_operation_num")));//医疗机构执业许可登记号
        String lcnsPrctcStartDate = StringUtil.valueOfString(paramMap.get("lcns_prctc_start_date"));//执业许可证有效期开始
        if(StringUtil.isNotEmpty(lcnsPrctcStartDate)){
            psaa0001.setLcnsPrctcStartDate(DateTimeUtil.strToDate(lcnsPrctcStartDate));
        }
        String lcnsPrctcEndDate = StringUtil.valueOfString(paramMap.get("lcns_prctc_end_date"));//执业许可证有效期结束
        if(StringUtil.isNotEmpty(lcnsPrctcEndDate)){
            psaa0001.setLcnsPrctcEndDate(DateTimeUtil.strToDate(lcnsPrctcEndDate));
        }
        psaa0001.setMedicalLevel(StringUtil.valueOfString(paramMap.get("medical_level")));//等级
        psaa0001.setSubjectionRelation(StringUtil.valueOfString(paramMap.get("subjection_relation")));//隶属关系
        psaa0001.setBedNum(StringUtil.valueOfString(paramMap.get("bed_num")));//床位数
        psaa0001.setOwnership(NumberUtil.safeToNumber(paramMap.get("ownership")));//所有制形式
        psaa0001.setCompetebtOrg(StringUtil.valueOfString(paramMap.get("competebt_org")));//主管单位
        psaa0001.setMedicalType(StringUtil.valueOfString(paramMap.get("medical_type")));//医疗机构类别
        psaa0001.setTheirCity(StringUtil.valueOfString(paramMap.get("their_city")));//所属地市
        psaa0001.setTheirCounty(StringUtil.valueOfString(paramMap.get("their_county")));//所属区县
        psaa0001.setProsecutionArea(StringUtil.valueOfString(paramMap.get("prosecution_area")));//经营资质信息
        psaa0001.setAddress(StringUtil.valueOfString(paramMap.get("address")));//医疗机构地址
        psaa0001.setTel(StringUtil.valueOfString(paramMap.get("tel")));//单位电话
        psaa0001.setZip(StringUtil.valueOfString(paramMap.get("zip")));//邮政编码
        psaa0001.setUrl(StringUtil.valueOfString(paramMap.get("url")));//网址
        psaa0001.setDeclarePerson(StringUtil.valueOfString(paramMap.get("declare_person")));//信息申报联系人
        psaa0001.setDeclareMoble(StringUtil.valueOfString(paramMap.get("declare_moble")));//联系人手机
        psaa0001.setDeclareEmail(StringUtil.valueOfString(paramMap.get("declare_email")));//联系人邮箱
        if(psaa0001Mapper.updateByPrimaryKeySelective(psaa0001) > 0){
            return ServerResponse.createBySuccessMessage(Const.Msg.UPDATESUCCESS);
        }
        return ServerResponse.createByErrorMessage(Const.Msg.UPDATEERROR);
    }
}
